package com.wlin.leaf;

import lombok.extern.slf4j.Slf4j;
import me.wlin.leaf.TwitterIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 项目名称：leafsnow
 * 类名称：IdWorkerAutoConfiguration
 * 类描述： ${DESCRIPTION}
 *
 * @author wlin
 * @version 1.0
 * @since 2019/4/10
 */
@Configuration
@ConditionalOnMissingBean(TwitterIdWorker.class)
@ConditionalOnProperty(prefix = "idworker",name = "enable",havingValue = "true")
@EnableConfigurationProperties(IdWorkerProperties.class)
@Slf4j
public class IdWorkerAutoConfiguration {

    @Autowired
    private IdWorkerProperties idWorkerProperties;

    @Configuration
    @ConditionalOnProperty(prefix = "idworker",name = "strategyClass",havingValue = "com.wlin.leaf.ZkWorkIDStrategy")
    @EnableConfigurationProperties(ZkWorkIDProperties.class)
    public class ZkStrategyCreator{

        @Autowired
        private ZkWorkIDProperties zkWorkIDProperties;

        @Bean
        public TwitterIdWorker worker() throws Exception {
            WorkerIdObtainStrategy workerIdGetterStrategy = new ZkWorkIDStrategy(zkWorkIDProperties);
            WorkerIdGetter workerIdGetter = new WorkerIdGetter(workerIdGetterStrategy);
            Integer workId = workerIdGetter.getWorkId();
            log.info("TwitterIdWorker initialized ... strategy {} ,workId {}",idWorkerProperties.getStrategyClass(),workId);
            return TwitterIdWorker.getInstance(workId);
        }
        
    }




}
